package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import business.PariHistorique;
import business.Utilisateur;
import business.utilities.PariHistorique_Package;

import com.cedarsoftware.util.io.JsonWriter;


public class S_Bet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		Utilisateur utilisateur = (Utilisateur) request.getSession().getAttribute("utilisateur");
		
		// Renvoie l'historique des paris.
		List<PariHistorique> parisHistorique = utilisateur.getParisHistorique();
		if(parisHistorique != null && !parisHistorique.isEmpty()) {
			List<PariHistorique_Package> json = new ArrayList<PariHistorique_Package>();
			for(PariHistorique ph : parisHistorique) {
				PariHistorique_Package php = new PariHistorique_Package();
				php.update(ph);
				json.add(php);
			}
			out.print(JsonWriter.objectToJson(json));
		}
		else
			out.print("{}");
		
		out.close();
	}
}